Skip to content

Method: chooseRandomMove(VGPlayer, VGState)

1: package de.fhdw.gaming.ipspiel22.vierGewinnt.domain.impl;
2:
3: import java.util.Arrays;
4: import java.util.List;
5: import java.util.Map;
6: import java.util.Optional;
7: import java.util.Random;
8:
9: import de.fhdw.gaming.core.domain.DefaultGame;
10: import de.fhdw.gaming.core.domain.ObserverFactoryProvider;
11: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGGame;
12: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGMoveChecker;
13: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGPlayer;
14: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGState;
15: import de.fhdw.gaming.ipspiel22.vierGewinnt.domain.VGStrategy;
16: import de.fhdw.gaming.ipspiel22.vierGewinnt.moves.VGMove;
17: import de.fhdw.gaming.ipspiel22.vierGewinnt.moves.factory.VGMoveFactory;
18: import de.fhdw.gaming.ipspiel22.vierGewinnt.moves.impl.VGDefaultMoveFactory;
19:
20: /**
21: * Implements the Vier gewinnt game.
22: */
23: public class VGGameImpl extends DefaultGame<VGPlayer, VGState, VGMove, VGStrategy> implements VGGame {
24: /**
25: * For choosing a random column.
26: */
27: private static final Random RANDOM = new Random();
28:
29: /**
30: * The move factory.
31: */
32: private final VGMoveFactory moveFactory;
33:
34: /**
35: * Creates a Vier gewinnt game.
36: *
37: * @param id The ID of this game.
38: * @param initialState The initial state of the game.
39: * @param strategies The players' strategies.
40: * @param maxComputationTimePerMove The maximum computation time per move in seconds.
41: * @param moveChecker The move checker.
42: * @param observerFactoryProvider The {@link ObserverFactoryProvider}.
43: * @throws IllegalArgumentException if the player sets do not match.
44: * @throws InterruptedException if creating the game has been interrupted.
45: */
46: VGGameImpl(final int id, final VGState initialState, final Map<String, VGStrategy> strategies,
47: final long maxComputationTimePerMove, final VGMoveChecker moveChecker,
48: final ObserverFactoryProvider observerFactoryProvider)
49: throws IllegalArgumentException, InterruptedException {
50:
51: super(id, initialState, strategies, maxComputationTimePerMove, moveChecker, observerFactoryProvider);
52: this.moveFactory = new VGDefaultMoveFactory();
53: }
54:
55: @Override
56: public Optional<VGMove> chooseRandomMove(final VGPlayer player, final VGState state) {
57: final List<VGMove> movesList = Arrays.asList(this.moveFactory.createFirstColumnMove(),
58: this.moveFactory.createSecondColumnMove(),
59: this.moveFactory.createThirdColumnMove(),
60: this.moveFactory.createFourthColumnMove(),
61: this.moveFactory.createFifthColumnMove(),
62: this.moveFactory.createSixthColumnMove(),
63: this.moveFactory.createSeventhColumnMove());
64: final int index = RANDOM.nextInt(movesList.size() - 1);
65: return Optional.of(movesList.get(index)); //TODO anschauen
66: }
67:
68: @Override
69: public String toString() {
70: return String.format("VGGame[id=%d, %s]", this.getId(), this.gameToString());
71: }
72: }